<?php
defined( '_JEXEC' ) or die( 'Restricted Access' );
/**
 * @version     $Id$
 * @package     Virtuemart
 * @subpackage  SemanticWeb Module
 * @copyright   Copyright (C) 2009 Alex Stolz - All rights reserved.
 * @license     GNU/GPL v2
 * SemanticWeb is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.
 
 * SemanticWeb is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with SemanticWeb; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

/*
// this commented block is needed iff create_manifest.php is called from console

// Set flag that this is a parent file
define('_JEXEC', 1);

define('JPATH_BASE', dirname(__FILE__));

define('DS', DIRECTORY_SEPARATOR);

require_once(JPATH_BASE.DS.'configuration.php');
require_once(JPATH_BASE.DS.'includes'.DS.'defines.php');
require_once(JPATH_BASE.DS.'includes'.DS.'framework.php');
require_once(JPATH_BASE.DS.'libraries'.DS.'joomla'.DS.'factory.php');
require_once(JPATH_BASE.DS.'arc'.DS.'ARC2.php');
*/

// configure the ARC2 framework (used for SparQL queries)
$config = new JConfig; // configuration data, get db information from it
$config_arc = array(
  // db
  'db_host' => $config->host,
  'db_name' => $config->db,
  'db_user' => $config->user,
  'db_pwd' => $config->password,
  // store name = table prefix
  'store_name' => $config->dbprefix.'arc',
);
unset($config);
$store = ARC2::getStore($config_arc);
if (!$store->isSetUp()) {
  $store->setUp(); // create mysql tables
}
$store->query('LOAD <http://purl.org/goodrelations/v1#>');


/* creates a mod_semanticweb.xml file */
$manifest = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$manifest .= "<install type=\"module\" version=\"1.5.0\">\n";
$manifest .= "	<name>Semantic Web Extension</name>\n";
$manifest .= "	<author>Alex Stolz</author>\n";
$manifest .= "	<copyright>(C) ".date("Y", time())." Stalsoft, GNU GPL</copyright>\n";
$manifest .= "	<license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>\n";
$manifest .= "	<authorEmail>development@stalsoft.com</authorEmail>\n";
$manifest .= "	<creationDate>".date("d.m.Y", time())."</creationDate>\n";
$manifest .= "	<authorUrl>http://www.stalsoft.com</authorUrl>\n";
$manifest .= "	<version>1.0</version>\n";
$manifest .= "	<description>Semantic Web extension for VirtueMart (compatible with Virtuemart 1.1.x and Joomla! 1.5.x)</description>\n";
$manifest .= "	<files>\n";
$manifest .= "		<filename module=\"mod_semanticweb\">mod_semanticweb.php</filename>\n";
$manifest .= "		<filename>index.html</filename>\n";
$manifest .= "		<filename>helper.php</filename>\n";
$manifest .= "		<filename>create_manifest.php</filename>\n";
$manifest .= "		<folder>tmpl</folder>\n";
$manifest .= "		<folder>arc</folder>\n";
$manifest .= "		<folder>img</folder>\n";
$manifest .= "	</files>\n";
$manifest .= "	<params group=\"advanced\">\n";
$manifest .= "		<param name=\"special_validity\" type=\"text\" default=\"1\" label=\"Special validity duration\" description=\"Defines a global validity duration for specials (in days). Though, product-specific expiration date has priority. A value of 0 means no expiration\" />\n";
$manifest .= "		<param name=\"offering_validity\" type=\"text\" default=\"1\" label=\"Offering validity duration\" description=\"Defines a global validity duration for offerings (in days). Though, product-specific expiration date has priority. A value of 0 means no expiration\" />\n";
$manifest .= "		<param name=\"@spacer\" type=\"spacer\" label=\"\" default=\"\" description=\"\" />\n";
$manifest .= "		<param name=\"uom_lwh\" type=\"list\" default=\"CMT\" label=\"Unit of measurement for dimensions\" description=\"Select the default unit of measurement for length, width and height dimensions\">\n";
$manifest .= "			<option value=\"MMT\">MMT (millimetre)</option>\n";
$manifest .= "			<option value=\"CMT\">CMT (centimetre)</option>\n";
$manifest .= "			<option value=\"DMT\">DMT (decimetre)</option>\n";
$manifest .= "			<option value=\"MTR\">MTR (metre)</option>\n";
$manifest .= "			<option value=\"HMT\">HMT (hectometre)</option>\n";
$manifest .= "			<option value=\"KMT\">KMT (kilometre)</option>\n";
$manifest .= "			<option value=\"INH\">INH (inch)</option>\n";
$manifest .= "			<option value=\"77\">77 (milli-inch)</option>\n";
$manifest .= "			<option value=\"FOT\">FOT (foot)</option>\n";
$manifest .= "		</param>\n";
$manifest .= "		<param name=\"uom_weight\" type=\"list\" default=\"GRM\" label=\"Unit of measurement for weight\" description=\"Select the default unit of measurement for describing product weights\">\n";
$manifest .= "			<option value=\"MGM\">MGM (milligram)</option>\n";
$manifest .= "			<option value=\"CGM\">CGM (centigram)</option>\n";
$manifest .= "			<option value=\"DG\">DG (decigram)</option>\n";
$manifest .= "			<option value=\"GRM\">GRM (gram)</option>\n";
$manifest .= "			<option value=\"DJ\">DJ (decagram)</option>\n";
$manifest .= "			<option value=\"KGM\">KGM (kilogram)</option>\n";
$manifest .= "			<option value=\"TNE\">TNE (tonne)</option>\n";
$manifest .= "			<option value=\"LBR\">LBR (pound)</option>\n";
$manifest .= "			<option value=\"GRN\">GRN (grain)</option>\n";
$manifest .= "			<option value=\"ONZ\">ONZ (ounce)</option>\n";
$manifest .= "		</param>\n";
$manifest .= "		<param name=\"@spacer\" type=\"spacer\" label=\"\" default=\"\" description=\"\" />\n";
$manifest .= "		<param name=\"eligibleRegions\" type=\"text\" default=\"\" label=\"Eligible regions (example: DE,IT,AT,GB)\" description=\"Here ones can define the eligible regions for the product offerings. The country codes (ISO 3166-1 or ISO 3166-2) have to be comma-separated.\" />\n";

// from sparql
$configuration_done = 0; // be sure, that the user has at least once seen the goodrelations master configuration data
$elements = array(
	'BusinessFunction' => 'Business Function',
	'BusinessEntityType' => 'Eligible Customer Type',
	'DeliveryMethod' => 'Available Delivery Method',
	'PaymentMethod' => 'Accepted Payment Method',
	'WarrantyScope' => 'Warranty Promise'
);
foreach($elements as $search => $alias) {
	$search_id = strtolower($search);
	// sparql query
	$q = '
	PREFIX gr: <http://purl.org/goodrelations/v1#> .
	SELECT ?'.$search_id.' ?description WHERE {
		{ ?'.$search_id.' a gr:'.$search.'; rdfs:comment ?description } UNION
		{ ?'.$search_id.' a ?subclass .
		  ?subclass rdfs:subClassOf gr:'.$search.' . }
	}
	';
	if ($rows = $store->query($q, 'rows')) {
		$manifest .= "		<param name=\"@spacer\" type=\"spacer\" label=\"\" default=\"\" description=\"\" />\n";
		if(eregi('BusinessFunction', $search)) {
			$manifest .= "		<param name=\"".$search."\" type=\"list\" default=\"Sell\" label=\"$alias\" description=\"Select a $search from the list\">\n";
			foreach ($rows as $row) {
				$item = ereg_replace('^.*#', '', $row[$search_id]);
				$manifest .= "			<option value=\"$item\">$item</option>\n";
			}
			$manifest .= "		</param>\n";
		}
		else {
			foreach ($rows as $row) {
				$item = ereg_replace('^.*#', '', $row[$search_id]);
				if(eregi('WarrantyScope', $search)) $item_arr[] = $item;
				$manifest .= "		<param name=\"".$search."_$item\" type=\"radio\" default=\"0\" label=\"$alias *$item*\" description=\"".($row['description']?htmlentities($row['description']):"Enable the $search $item")."\">\n";
				$manifest .= "			<option value=\"0\">No</option>\n";
				$manifest .= "			<option value=\"1\">Yes</option>\n";
				$manifest .= "		</param>\n";
				if(eregi('WarrantyScope', $search)) {
					$manifest .= "		<param name=\"".$item."_duration\" type=\"text\" default=\"24\" label=\"Warranty duration for $item (in months)\" description=\"If the upper warranty item is enabled, you can define here the warranty duration (in months).\" />\n";
				}
			}
		}
		$configuration_done = 1;
	}
}
$manifest .= "	</params>\n";
$manifest .= "	<params>\n";
$manifest .= "		<param name=\"configuration_done\" type=\"hidden\" default=\"$configuration_done\" />\n";
$manifest .= "		<param name=\"owl_filename\" type=\"text\" default=\"semanticweb.owl\" label=\"Name of OWL file\" description=\"Here you can define the name of your OWL file that will be produced\" />\n";
$manifest .= "		<param name=\"alink\" type=\"text\" default=\"Semantic Web RDF/XML\" label=\"Link title\" description=\"Define the link text shown on the frontend page\" />\n";
$manifest .= "		<param name=\"aimg\" type=\"imagelist\" label=\"Link image\" description=\"Select a linked image shown on the frontend page\" directory=\"/modules/mod_semanticweb/img\" default=\"-1\" hide_default=\"1\" />\n";
$manifest .= "		<param name=\"timeout\" type=\"text\" default=\"0\" label=\"File refresh period\" description=\"the timeout in seconds before a cached version is refreshed (defaults to 3600 = 1 hour)\" />\n";
$manifest .= "	</params>\n";
$manifest .= "</install>\n";

// save installer file
$manifestFile = fopen(dirname(__FILE__).DS."mod_semanticweb.xml", "w+");
if ($manifestFile) {
	fputs($manifestFile, $manifest);
	fclose($manifestFile);
} else {
	echo "<br /><b>Error creating extgr feed file, please check write permissions.</b><br />\n";
}

if ($store->isSetUp()) {
  $store->drop(); // drop mysql tables
  unset($store);
}
?>